/**
 * Copyright (c) 2009-2016 https://github.com/zhanglongjian
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.ggy.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

import com.ggy.utils.Log4jUtil;

/**
 * 字符过滤器
 * @description TODO
 * @author Michael
 * @createTime 2017年8月21日 上午9:44:20
 */
@WebFilter(filterName = "EncodingFilter", urlPatterns = { "/*" }, initParams = {
		@WebInitParam(name = "requestEncoding", value = "utf-8"),
		@WebInitParam(name = "responseEncoding", value = "text/html;charset=utf-8") })
public class EncodingFilter implements Filter {
	private String requestEncoding = "";
	private String responseEncoding = "";

	@Override
	public void destroy() {
		Log4jUtil.info("字符过滤器被销毁");

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		request.setCharacterEncoding(requestEncoding);
		response.setContentType(responseEncoding);
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		requestEncoding = config.getInitParameter("requestEncoding");
		responseEncoding = config.getInitParameter("responseEncoding");
		Log4jUtil.info("字符过滤器启动,请求字符编码初始化为:" + requestEncoding + ",响应字符编码初始化为:" + responseEncoding);

	}

}
